package com.example.javapoint.sync.block;

/**
 * 静态方法同步
 * 存在的意义：假设有线程t1 t2 操作Table 的实例对象obj1, 线程t3 t4操作Table 的实例对象obj2
 * 如果仅使用同步的方法t1 t3, t2 t4 可能存在问题, 所以需要解决同步问题就会涉及到静态同步
 */

class Table {
    synchronized static void print(int n) {
        for (int i = 1; i < 10; i++) {
            System.out.println(i * n);
            try {
                Thread.sleep(400);
            } catch (InterruptedException e) {
                System.out.println("Thread is interrupted");
            }
        }
    }
}

class Thread1 extends Thread{
    @Override
    public void run() {
        Table.print(1);
    }
}

class Thread2 extends Thread{
    @Override
    public void run() {
        Table.print(10);
    }
}

class Thread3 extends Thread{
    @Override
    public void run() {
        Table.print(100);
    }
}

class Thread4 extends Thread{
    @Override
    public void run() {
        Table.print(1000);
    }
}

public class TestStaticSync1 {
    public static void main(String[] args) {
        Thread1 thread1 = new Thread1();
        Thread2 thread2 = new Thread2();
        Thread3 thread3 = new Thread3();
        Thread4 thread4 = new Thread4();

        thread1.start();
        thread2.start();
        thread3.start();
        thread4.start();
    }
}
